package lhc.alg.top100;

/**
 * description: https://leetcode.cn/problems/search-in-rotated-sorted-array/
 *  搜索旋转排序数组  二分
 * author: hongchen.liao
 * date:  2022/9/23
 */
public class _33_ {


    class Solution {
        public int search(int[] nums, int target) {
            int n = nums.length;
            int l = 0, r = n-1;
            while(l <= r){
                int mid = l + (r-l)/2;
                if(nums[mid] == target){
                    return mid;
                }
                if(nums[0] <= nums[mid]){
                    if(nums[mid] > target && target >= nums[0]){
                        r = mid-1;
                    }else{
                        l = mid+1;
                    }
                }else{
                    if(nums[mid] < target && target <= nums[n-1]){
                        l = mid+1;
                    }else{
                        r = mid-1;
                    }
                }
            }
            return -1;
        }
    }


}
